{layout name="public/layout" /}
{include file='yxnavbar'}


<div class="main">
    {include file='../../plugin/gift/template/web/common_nav'}
    <div class="panel panel-info">
        <div class="panel-heading">当前数量： {$page_total}</div>
        <div class="panel-body">
            <form id="myform" action="./index.php" method="get" class="form-horizontal" role="form">
                <input type="hidden" name="c" value="site" />
                <input type="hidden" name="a" value="entry" />
                <input type="hidden" name="m" value="{$_P['m']}" />
                <input type="hidden" name="do" value="web" />
                <input type="hidden" name="r" value="gift.prize.main" />
                <div class="form-group clearfix">
                    <label class="col-xs-12 col-sm-3 col-md-1 control-label">关键字</label>
                    <div class="col-sm-4 col-xs-4 col-md-4">
                        <input class="form-control" name="search_data" placeholder="输入奖品名称" type="text" value="{$search_data}">
                    </div>
                    <div class="col-sm-3 col-lg-3 col-md-3 col-xs-6">
                        <button type="submit"  class="btn btn-primary"><i class="fa fa-search"></i>搜索</button>
                        <input type="hidden" name="__token__" value="{$Request.token}"/>
                    </div>
                    <div class="col-sm-2 col-lg-2 col-md-2 col-xs-2">
                        <input class="btn btn-success" type="button"  id="open_btn_first" value="新增礼品">
                    </div>
                    {if count($themelist)>1 }
                    <div class="col-xs-12 col-sm-2 col-md-2" style="margin-top: 10px">
                        <select  class="form-control" name="themeid" id="themeid">
                            {loop $themelist $row}
                             <option value="{$row['id']}" {if $themeid == $row['id'] }selected="selected"{/if}>{$row['id']} - {php echo mb_substr($row['title'],0,8,'utf-8');}</option>
                            {/loop}
                        </select>
                    </div>
                    {else}
                    <input type="hidden" name="themeid" value="{$themeid}" />
                    {/if}
                </div>
            </form>
        </div>
    </div>

    <div class="tab-content">
        <div class="panel panel-default">
            <div class="panel-body table-responsive">
                        <table class="table table-hover">
                            <thead class="navbar-inner">
                            <tr>
                                <th style="width:50px;">序号</th>
                                <th style="width:120px;">头图</th>
                                <th style="width:120px;">名称</th>
                                <th style="width:120px;">价值</th>
                                {if $shangjia_list}
                                <th style="width:120px;">商家</th>
                                {/if}
                                <th style="width:120px;">类型</th>
                                <th style="width:100px; text-align:right;">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            {loop $gift_record $key $item}
                            <tr class="str_center">
                                <td class="str_center">{php echo $key+1;}</td>
                                <td class="str_center"><img src="{php echo tomedia($item['top_img']);}" style="width: 100px;height: 80px;padding: 1px;"></td>
                                <td class="str_center">{$item['name']}</td>
                                <td class="str_center">{$item['price']}</td>
                                {if $shangjia_list}
                                <td class="str_center">{$item['shangjia_name']}</td>
                                {/if}
                                <td class="str_center">
                                    {if $item['type']==1}
                                    <span class="label label-success">实物</span>
                                    {else}
                                    <span class="label label-danger">红包</span>
                                    {/if}
                                </td>
                                <td class="str_center" style="text-align:right;">
                                    <a class="btn btn-warning btn-sm" onclick="editGiftItem({$item['id']})" title="编辑">编辑</a>
                                    <a class="btn btn-danger btn-sm"  onclick="checkDelGift({$item['id']})" title="删除">删除</a>
                                </td>
                            </tr>
                            {/loop}
                            </tbody>
                        </table>
                    </div>
                </div>
    </div>
    <div id="show_form" style="display: none;width: 600px;margin:20px;">
        <form action="" name="show_form" id="gift_form" class="form-horizontal form">
            <input type="hidden" name="themeid" value="{$themeid}"/>
            <input type="hidden" name="id" id="id" value=""/>
            {if $shangjia_list}
            <div class="form-group">
                <label class="col-xs-12 col-sm-4 col-md-3 col-lg-2 control-label">所属商家</label>
                <div class="col-sm-8 col-xs-12">
                    <select class="form-control" name="shangjiaid" id="shangjiaid">
                        <option value="0"}>----------请选择商家----------</option>
                        {loop $shangjia_list  $val}
                        <option value="{$val['id']}">{$val['id']}-{$val['name']}</option>
                        {/loop}
                    </select>
                </div>
            </div>
            {/if}
            <div class="form-group">
                <label class="col-xs-12 col-sm-4 col-md-2 col-lg-2 control-label"><span style="color: red;">*</span>名称</label>
                <div class="col-sm-8 col-xs-12">
                    <input type="text" class="form-control"  placeholder="请输入礼品名称" name="name" id="name" value="">
                </div>
            </div>
            <div class="form-group">
                <label class="col-xs-12 col-sm-4 col-md-2 col-lg-2 control-label"><span style="color: red;">*</span>头图</label>
                <div class="col-sm-8 col-xs-12">
                    {php echo view_form_field_image('top_img');}
                    <span class="help-block">请上传330*330(宽*高)的高清图</span>
                </div>
            </div>
            <div class="form-group">
                <label class="col-xs-12 col-sm-4 col-md-2 col-lg-2 control-label">价值</label>
                <div class="col-sm-8 col-xs-12">
                    <input type="text" class="form-control"  placeholder="请输入礼品价值" name="price" id="price"  value="">
                </div>
            </div>
            {if $system['fenxiao']}
            <div class="form-group">
                <label class="col-xs-12 col-sm-4 col-md-2 col-lg-2 control-label">类型</label>
                <div class="col-sm-8 col-xs-12" style="padding-top: 6px;">
                    <input  name="type" value="1"  checked type="radio"> 实物
                    <input  name="type" value="2"  type="radio"> 红包
                </div>
            </div>
            {else}
            <input type="hidden" name="type" value="1">
            {/if}
            <input type="hidden" name="status" value="1">
            <div class="form-group">
                <div class="col-md-12">
                    <input name="button" onclick="giftAddOrEditItem()" type="button" value="提交" class="btn btn-primary col-lg-2">
                </div>
            </div>
        </form>
    </div>
</div>
{$pager}
<script type="text/javascript">
    let is_new_wxapp_version = "{php echo $is_new_wxapp_version;}";
    let softmode = "{php echo $system['softmode'];}";
    if (softmode!=0&&!is_new_wxapp_version){
        layer.confirm('当前绑定小程序不是新版本，请选择新版本小程序或更换活动后台', {
            title: "邀请礼配置提示",
            icon: 0,
            btn: ['已知晓'],
            btnAlign: "r",
        }, function(index, layero){
        });
    }
    function jump(){
        layer.msg('正在准备下载');
    }

    $("#open_btn_first").on("click",function () {
        $("#id").val('');
        $("#name").val('');
        $(".img-responsive").attr("src",'');
        $("[name='top_img']").val('');
        $("#price").val('');
        $(`input[name='type'][value='1']`).attr('checked','true');
        $(`input[name='status'][value='1']`).attr('checked','true');
        var index = layer.open({
            type:1,
            title:['添加礼品','font-size:18px;'],
            area:'700px',
            zIndex:2,
            content:$('#show_form')
        })
    });

    function editGiftItem(id){
        let postUrl = "{php echo createWebUrlPlugin('gift.config.editGift',array('themeid'=>$themeid));}";
        $.ajax({
            url:postUrl+"&id="+id,
            dataType:'json',
            method:'get',
            success:function (e) {
                let status = e.status;
                let result = e.result;
                let data = result.data;
                if(status==1){
                    $('#shangjiaid').val(data.shangjiaid);
                    $("#id").val(data.id);
                    $("#name").val(data.name);
                    $(".img-responsive").attr("src",data.top_img);
                    $("#price").val(data.price);
                    $("[name='top_img']").val(data.top_img);
                    $(`input[name='type'][value=${data.type}]`).attr('checked','true');
                    $(`input[name='status'][value=${data.status}]`).attr('checked','true');
                    var index = layer.open({
                        type:1,
                        title:['编辑礼品','font-size:18px;'],
                        area:'700px',
                        zIndex:2,
                        content:$('#show_form'),
                        cancel:function () {
                            $('#show_form').hide();
                        }
                    });
                }else{
                    layer.msg(result.msg);
                }
            },
            fail:function (err) {
                console.log(err);
            }
        });
    }

    function giftAddOrEditItem() {
        let postUrl = "{php echo createWebUrlPlugin('gift.prize.postGift',array('themeid'=>$themeid));}";
        let success_redirect = "{php echo createWebUrlPlugin('gift.prize',array('themeid'=>$themeid));}";
        let formData = $("#gift_form").serialize();
        $.ajax({
            url:postUrl,
            dataType:'json',
            method:'post',
            data:formData,
            success:function (e) {
                let status = e.status;
                let result = e.result;
                if (status!=1){
                    layer.msg(result.msg)
                }else{
                    layer.msg(result.msg);
                    window.location.href = success_redirect;
                }
            },
            fail:function (err) {
                console.log(err)
            }
        })
    }

    function checkDelGift(id){
        let postUrl = "{php echo createWebUrlPlugin('gift.prize.checkConfigGift',array('themeid'=>$themeid));}";
        $.ajax({
            url:postUrl,
            dataType:'json',
            method:'post',
            data:{gift_id:id},
            success:function (e) {
                let status = e.status;
                let result = e.result;
                if (status!=1){    //礼品存在配置 进行提示
                    layer.confirm(result+',确定删除?',{title:'确定删除'},
                        function(){
                            delGift(id);
                        },function(){
                            layer.msg('取消删除');
                        });
                }else{    //没有绑定配置  直接删除
                    delGift(id);
                }
            },
            fail:function (err) {
                console.log(err)
            }
        })
    }

    function delGift(id){
        let postUrl = "{php echo createWebUrlPlugin('gift.prize.delGift',array('themeid'=>$themeid));}";
        $.ajax({
            url:postUrl,
            dataType:'json',
            method:'post',
            data: {gift_id:id},
            success:function (e) {
                let result = e.result;
                layer.msg(result.msg,{time:2000},function(){
                    location.reload();
                });
            },
            fail:function (err) {
                console.log(err)
            }
        })
    }

    $('#themeid').change(function () {
        var val = $(this).val();
        location.href = "{php echo createWebUrlPlugin('gift.prize');}&themeid="+val;
    });
</script>
